package com.huarui.purchaseOrder.dao.impl;

import java.math.BigDecimal;
import java.sql.SQLException;
//import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.ewaf.framework.dao.impl.BaseDaoImpl;
//import org.ewaf.framework.exception.BizException;
import org.springframework.orm.ibatis.SqlMapClientCallback;
//import com.huarui.purchaseOrder.enums.ErrorCode;
import com.huarui.purchaseOrder.dao.IPurchaseOrderDetailDao;
import com.huarui.purchaseOrder.model.PurOrSaleOrderDetail;
//import com.huarui.quotation.error.ErrorCode;
import com.ibatis.sqlmap.client.SqlMapExecutor;

public class PurchaseOrderDetailDaoImpl extends BaseDaoImpl implements IPurchaseOrderDetailDao{

//	@Override
//	@SuppressWarnings("unchecked")
//	public List<Map<String, String>> getPurchaseOrderDetailsByCode(String code) {
//		return this.getSqlMapClientTemplate().queryForList(super.namespace + ".getOrderDetailsByCode", code);
//	}

//	@Override
//	@SuppressWarnings("unchecked")
//	public List<Map<String, String>> getPurchaseOrderDetailsByComplex(Map<String, String> pm) {
//		
//		return this.getSqlMapClientTemplate().queryForList(super.namespace + ".getOrderDetailsByComplex", pm);
//	}
	
	
	
//	@SuppressWarnings("unused")
//	private void putParams(Map<String, Object> map, String[] params) {
//		if (params == null) {
//			throw new BizException(ErrorCode.PARAMS_IS_NULL);
//		}
//		if (params.length != 10) {
//			throw new BizException(ErrorCode.PARAMS_NUM_ERROR);
//		}
//		try {
//			if ( params[0] != null && !"".equals(params[0].trim()) ) {
//				map.put("name", params[0].trim());
//			}
//			if ( params[1] != null && !"".equals(params[1].trim()) ) {
//				map.put("code", params[1].trim());
//			}
//			if ( params[2] != null && !"".equals(params[2].trim()) ) {
//				map.put("brand", params[2].trim());
//			}
//			if ( params[3] != null && !"".equals(params[3].trim()) ) {
//				map.put("origin", params[3].trim());
//			}
//			if ( params[4] != null && !"".equals(params[4].trim()) ) {
//				map.put("source", Integer.parseInt(params[4].trim()));
//			}
//			if ( params[5] != null && !"".equals(params[5].trim()) ) {
//				map.put("areaId", Integer.parseInt(params[5].trim()));
//			}
//		} catch (RuntimeException re) {
//			if (re instanceof ClassCastException) {
//				throw new BizException(ErrorCode.PARAMS_TYPE_ERROR);
//			} else if (re instanceof NumberFormatException) {
//				throw new BizException(ErrorCode.PARAMS_TYPE_ERROR);
//			} else {
//				throw new BizException(ErrorCode.PARAMS_OTHER_ERROR);
//			}
//		}
//	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PurOrSaleOrderDetail> getPurchaseOrderDetailsByOrderId(
			Integer id)  {
		
		return this.getSqlMapClientTemplate().queryForList(
				namespace + ".getPurchaseOrderDetailsByOrderId", id);
	}

	@Override
	public PurOrSaleOrderDetail getPurchaseOrderDetailsById(Integer id) {
		
		return (PurOrSaleOrderDetail) getSqlMapClientTemplate().queryForObject(namespace+".getById", id);
	}


//	@Override
//	public List<PurOrSaleOrderDetail> getPurchaseOrderDetailsByMap(Map map) {
//		return this.getSqlMapClientTemplate().queryForList(
//				namespace + ".getOrderDetailsByMap", map);
//	}


	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String,String>> getPurchaseOrderDetailByOrderIdJson(Integer id) {
		
		return (List<Map<String, String>>) getSqlMapClientTemplate().queryForList(namespace+".getOrderDetailByOrderIdJson", id);
	}

//	@Override
//	public List<PurOrSaleOrderDetail> getPurchaseOrderDetailsByOrderId(Map map) {
//		// TODO Auto-generated method stub
//		return null;
//	}

	@Override
	public BigDecimal getAmountByOrdersId(Integer ordersId) {
		return (BigDecimal) this.getSqlMapClientTemplate().queryForObject(super.namespace + ".getAmountByOrdersId", ordersId);
	}
	
	@Override
	public Integer addBatch(final List<PurOrSaleOrderDetail> list) {
		
		return (Integer) this.getSqlMapClientTemplate().execute(
			new SqlMapClientCallback() {
				@Override
				public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
					executor.startBatch();
					
					for(PurOrSaleOrderDetail pur : list) {
						executor.insert(namespace + ".create", pur);
					}
					
					Integer num = executor.executeBatch();
					
					return num;
				}
			}
		);
	}
	
	@Override
	public Integer updateBatch(final List<PurOrSaleOrderDetail> list) {

		return (Integer) this.getSqlMapClientTemplate().execute(
			new SqlMapClientCallback() {
				@Override
				public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
					executor.startBatch();
					for(PurOrSaleOrderDetail pur : list) {
						executor.update(namespace + ".update", pur);
					}
					
					Integer num = executor.executeBatch();
					
					return num;
				}
			}
		);
	}
	
	
	@Override
	public Integer delBatch(List<Integer> idList) {
		
		return this.getSqlMapClientTemplate().delete(namespace + ".deleteDetailsByIds", idList);
		
	}
	
	@Override
	public void delDetailsByOrderId(Integer ordersId) {
		this.getSqlMapClientTemplate().delete(super.namespace + ".delDetailsByOrderId", ordersId);
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> getOrderItems(Map<String, Object> map) {
		
		return getSqlMapClientTemplate().queryForList(namespace+".getOrderDetailByOrderId", map);
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> getPurDetailsInfoByComplex(Map<String, Object> map) {
		return this.getSqlMapClientTemplate().queryForList(super.namespace + ".getPurDetailsInfoByComplex", map);
	}
	
	@Override
	public Integer getPurDetailsCountByComplex(Map<String, Object> map) {
		return (Integer) this.getSqlMapClientTemplate().queryForObject(super.namespace + ".getPurDetailsCountByComplex", map);
	}
	

	@Override
	@SuppressWarnings("unchecked")
	public List<Integer> getDetailsIdsByOrderId(Integer orderId) {
		return this.getSqlMapClientTemplate().queryForList(super.namespace + ".getDetailsIdsByOrderId", orderId);
	}
}
